<?php
/*
  Microblog bot for game release notifications
  Copyright (C) 2017  Alexander Yakovlev

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Source;

use \Game;

class Textadventures extends Source {
  public $title = "Textadventures.co.uk";
  protected function parse() {
    $text = $this->get_text('http://textadventures.co.uk/games/latest');
    $this->dom->loadStr($text, []);
    unset($text);
    $games = $this->dom->find('.games-item');
    foreach ($games as $gameBlock) {
      $game = new Game;
      $game->url = 'http://textadventures.co.uk'.$gameBlock->find('.games-title a')->getAttribute('href');
      $lines = $this->dom->find('.game_info_panel_widget tr');
      $game->title = $gameBlock->find('.games-title a')->innerHtml;
      $date = strtotime($gameBlock->find('.games-date')->innerHtml);
      if ($date < $this->period) continue;
      $game_page = new \PHPHtmlParser\Dom;
      $text = $this->get_text($game->url);
      $game_page->loadStr($text, []);
      unset($text);
      try {
        $game->author = str_replace('by ', '', $game_page->find('h1 small')->innerHtml);
        $desc = $game_page->find('.col-md-12 .col-md-10 .col-md-7')[0];
        if ($desc) {
          $desc = strip_tags($desc->innerHtml);
          $game->description = trim(str_replace(
            'You are not logged in.   If you log in before playing, you\'ll be able to save your progress - which means you can come back later and pick up where you left off.  Log in    Play online',
            '',
            $desc
          ));
          $game->description = trim(str_replace(
            ' Play online',
            '',
            $desc
          ));
        }
      } catch (\Exception $e) {} // probably a 18+ game, no info on game page

      $this->games[] = $game;
    }
  }
}
